package com.thenie.chat.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description RabbitMQ路由、队列配置
 * @Author thenie
 * @Data 2022/6/5 11:34
 */
@Configuration
public class RabbitmqConfig {

    //路由器名称
    public static final String EXCHANGE_DIRECT_ADD_REQUEST = "exchange_direct_add_request";

    public static final String EXCHANGE_DIRECT_ONLINE = "exchange_direct_online";

    //更新数据请求状态队列
    public static final String QUEUE_ADD_REQUEST_SAVE = "queue_add_request_save";

    //发送好友请求消息队列
    public static final String QUEUE_ADD_REQUEST_SEND = "queue_add_request_send";

    //广播上线队列
    public static final String QUEUE_ONLINE_UP = "queue_online_up";

    //广播下线队列
    public static final String QUEUE_ONLINE_DOWN = "queue_online_down";


    /**
     * 声明路由器
     * @return
     */
    @Bean(EXCHANGE_DIRECT_ADD_REQUEST)
    public DirectExchange exchangeDirectAddRequest(){
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT_ADD_REQUEST).durable(true).build();
    }
    @Bean(EXCHANGE_DIRECT_ONLINE)
    public DirectExchange exchangeOnline(){
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT_ONLINE).durable(true).build();
    }

    /**
     * 声明队列
     * @return
     */
    @Bean(QUEUE_ADD_REQUEST_SAVE)
    public Queue queueAddRequestSave(){
        return new Queue(QUEUE_ADD_REQUEST_SAVE);
    }

    @Bean(QUEUE_ADD_REQUEST_SEND)
    public Queue queueAddRequestSend(){
        return new Queue(QUEUE_ADD_REQUEST_SEND);
    }

    @Bean(QUEUE_ONLINE_UP)
    public Queue queueOnlineUp(){
        return new Queue(QUEUE_ONLINE_UP);
    }

    @Bean(QUEUE_ONLINE_DOWN)
    public Queue queueOnlineDown(){
        return new Queue(QUEUE_ONLINE_DOWN);
    }

    /**
     * 绑定到交换机
     * @param exchange
     * @param queue
     * @return
     */
    @Bean
    public Binding bindingQueueAddRequestSave(@Qualifier(EXCHANGE_DIRECT_ADD_REQUEST) Exchange exchange,
                                              @Qualifier(QUEUE_ADD_REQUEST_SAVE) Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("save").noargs();
    }

    @Bean
    public Binding bindingQueueAddRequestSend(@Qualifier(EXCHANGE_DIRECT_ADD_REQUEST) Exchange exchange,
                                              @Qualifier(QUEUE_ADD_REQUEST_SEND) Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("send").noargs();
    }

    @Bean
    public Binding bindingQueueOnlineUp(@Qualifier(EXCHANGE_DIRECT_ONLINE) Exchange exchange,
                                              @Qualifier(QUEUE_ONLINE_UP) Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("onlineUp").noargs();
    }
    @Bean
    public Binding bindingQueueOnlineDown(@Qualifier(EXCHANGE_DIRECT_ONLINE) Exchange exchange,
                                              @Qualifier(QUEUE_ONLINE_DOWN) Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("onlineDown").noargs();
    }



}
